#include "StdAfx.h"
#include "CustomProtocal.h"

static xgc_uint16	custom_header_space( xgc_lpvoid context )
{
	return sizeof(custom_message_header);
}

static xgc_uint32	custom_packet_length( const xgc_byte* header )
{
	// return packet length
	const custom_message_header *custom_header = reinterpret_cast< const custom_message_header* >( header );
	return custom_header->length;
}

static xgc_uint32	custom_packet_filter( const xgc_byte* header )	// filter your interesting packet, return event code
{
	const custom_message_header *custom_header = reinterpret_cast< const custom_message_header* >( header );

	if( custom_header->type == SYSTEM_MESSAGE_TYPE )
	{
		switch( custom_header->code )
		{
		case EVENT_PING:
			return FILTER_SYSTEM_PING;
			break;
		case EVENT_PONG:
			return FILTER_SYSTEM_PONG;
			break;
		}
	}
	return FILTER_PASS;
}

static xgc_uint32	custom_packet_system( xgc_byte event, xgc_byte* header )		// return packet base address;
{
	custom_message_header *custom_header = reinterpret_cast< custom_message_header* >( header );

	custom_header->uid = -1;
	custom_header->type = SYSTEM_MESSAGE_TYPE;
	custom_header->code = event;

	return sizeof(custom_message_header );
}

static xgc_uint32	custom_packet_finial( xgc_byte* header, xgc_uint32 length )
{
	custom_message_header *custom_header = reinterpret_cast< custom_message_header* >( header );
	custom_header->length = length;

	return length;
}

net::PacketProtocal ProtocalCustom = 
{
	custom_header_space,
	custom_packet_length,
	custom_packet_filter,
	custom_packet_system,
	custom_packet_finial,
	xgc_nullptr, //custom_packet_verifiy,
	xgc_nullptr, //custom_packet_decrypt,
	xgc_nullptr, //custom_packet_encrypt,
};
